Hello world

Try me

Open In ColabBinder

Introduction

Ok, ok, I know, we promised this book would be interactive and you want to get your hands dirty, so with no further ado, let us start programming! Note that this page is a little bit different than the introduction, because it integrates two badges to open this page´s Notebook in Colabs or Binder. There is a lot to unpack here, so let´s explain what these concepts are first.

A Jupyter Notebook is a file that runs on Jupyter, which is an open platform for interactive, distributed computing. When you open a Jupyter Notebook on a web browser, what you see is text descriptions, code, math, graphs, and multi-media content. The code is interactive, meaning that you can just execute it from the web interface, making it an outstanding tool to share software and scientific results. The main advantages of Jupyter Notebooks are:

  1. Explainability: Jupyter Notebooks combine software code together with rich multimedia descriptions. This allows researchers to explain their results, show exactly the code the have used, describe its fundamentals and its results. This narrative format helps the reader understand and in extension reuse scientific results.

  2. Composability: You can easily combine different Jupyter Notebooks into larger project. This book is a good example! It is 100% built with Jupyter Notebooks.

  3. Community Support: There is a very large user community worldwide supporting Jupyter Notebooks. There are lots of open Jupyter Notebooks specifically build to learn computer programming, applied mathematics, or data science.

Google Colabs is a Jupyter server provided by Google so that scientists can develop and exchange Jupyter Notebooks, and it only requires a valid Google account. Similarly, Binder provides a Jupyter server environment to share Jupyter Notebooks that are stored in a code repository (another concept that we will concept extensively later in this book). Since, this book is consisted of Jupyter Notebooks stored in an open repository, you can either open them in Google Colabs or in Binder, just by clicking on the corresponding badges you can find at the top of the page.

So, with no further ado, click on either badge to continue with this Hello World tutorial.

Notebook Files

Jupyter Notebook is a web file based system, so you are now reading a Notebook file that someone wrote sometime ago and stored somewhere, in this case in a repository. Jupyter Notebooks are composed of cells. You can think of cells as sections of code that are executed by the Jupyter server. There are two types of cells:

  • Markdown cells: Markdown is a declarative programming language that is used to edit formatted text, text with styles (titles, tables, hyper-links) and embedded multimedia content (images and videos). Normally, these are cells that will contain the documentation of our code, including text, math, and images. The Notebook Working with Markdown Cells contains a tutorial on how to write Markdown cells, but for now, if you want to get a hint of how Markdown cells look like, just double-click on this cell.

  • Code cells: Code cells contain sections of code that are executed by the Jupyter server. Code cells are interactive, they can ask for user input and user output, as you will learn below in the code cells included in this Hello World tutorial.

Executing Code Cells

Ok, after this brief introduction, we are ready to execute our code cell below. This cell will just show in the output the message “Hello World”, using the function print, which is a built-in function in Python that prints a text message in the output.

[2]:
print("Hello World")
Hello World

Look for the button with the Play icon (\(\blacktriangleright\)), either to the left of the cell or in the top menu, and press it to execute the code cell. You should see the message “Hello World” in the output. You can also try to edit the message to a different message and execute the code cell again to see what happens, but please, mind your language!

Editing the Notebook and saving your work

Note that, as noted above, you can edit any cell in the Notebook just by double-clicking on it. In subsequent tutorials, we will learn how to edit Markdown cells and obviously, how to write code cells in Python, but for now, just bear in mind that you can make changes to the cells in the Notebook, for instance to complete the exercises you will find in the tutorials. Note also that you can also add or remove cells. For instance, in colabs, you just need to click on the buttons with the plus icon (\(+\)) in the topbar menu to add a Code Cell (\(+\) Code) or a Markdown cell (\(+\) Text) below a selected cell:

Colabs top bar menu

In Binder, it is just the same, you just need to click on the button with the plus icon (\(+\)) in the topbar menu to add a cell (by default, a code cell), then you can Click on the type selector to the right to change the type to Markdown if you want to add a Markdown cell instead:

Binder top bar menu

To remove a cell, you can click on the trash icon in the topbar menu (Binder) or in the cell menu in the top right of the cell (Colabs).

☝ Note the little doggie in the Colabs menu? His name is Corgie, and you can meet him if you enable this options in the Colabs settings. If you are not a dog person, there are other options for you to explore. Now you can take some time to explore the different options available in the menu on your own.

Saving your work

Now, the bad news. The changes you just made are not saved in the repository, and they are only available in the runtime environment of the Notebook. If you want to keep a copy of the changes, you need to save a copy of the Notebook. The simplest way to do this is in Colabs, by clicking on the ‘Copy to Drive’ button in the topbar menu of Colabs, or by opening the file menu and selecting ‘Save a copy in Drive’.

In binder, the process is a bit more convoluted. You need to click on the ‘folder’ icon in the lateral menu, right-click the file you want to download and click the download button. This will download a copy of the Notebook into your computer.

Saving the images locally

If you are puzzled about the process, perhaps the next section can cast some light on it.

What just happened in the background?

Ok, we know that Jupyter Notebooks provide a file format and a web environment to share interactive code and documentation. But how does this work in practice? The intention of this section is not to provide a complete explanation of the Jupyter Notebook system, but to give you a quick overview of how it works. We will revisit this overview in the future.

In a nutshell, the Jupyter ecosystem follows the same design principles that guided the design of the Internet to generate and distribute scientific results. Let´s explain these principles using an example. When you use a web browser to search for results in Google, the browser connects to a service of Google (Google Search Engine) that lists results related to your search query.

searching for cats playing piano in Google

Each result is a web page, which in the end is not very different from a Jupyter Notebook. Each page has an index file (written in a markup language called Hypertext Markup Language, or HTML) that indexes the content of the page (text, video, other HTML files, etc.) The index filed and all the indexed content are referred to as resources and each one of them has a unique location in the Internet (Unique Resource Location or URL in short). Your browser uses Internet Protocols to access these resources, which are distributed in a networks of servers (called Content Delivery Networks, or CDNs) across the Internet.

Jupyter adopts a similar model to implement a distributed model for interactive computation. The Jupyter Notebook file specifies the contents and code to be executed, and the Jupyter server shares the files, interprets the contents and serves them to the browser in the right format so that you can visualize it, run it, and edit it. To run the code cells, The Jupyter system deploys what are known as kernels. The communication protocols used by Jupyter Notebooks allow the server to pass the code in code cells to the kernel, and represent the output provided by the Kernel in the Notebook, similar to how a web page renders images or videos served from a CDN. In this course we will use Python kernels, but Jupyter supports other kernels, such as [R](https://en.wikipedia.org/wiki/R_(programming_language) kernels, or Julia kernels.

Jupyter server

So, when you open a Jupyter Notebook in Colabs or Binder, the service providers make available for you a sandbox Jupyter environment, an isolated Jupyter environment with a dedicated file system, dedicated Jupyter server, and dedicated Python Kernels. Kernels execute the code, servers host and interpret the Notebook files and the browser renders the file and presents the result. In the top bar of Colabs or Binder, you can see the status of the kernels, or browse the file system available to your Jupyter environment. And all this wonder is 100% free. The Jupyter Ecosystem is open source and neither Google nor Binder charge for the computing resources they make available everytime someone uses their services, so let´s just take one minute to appreciate it.